package com.zm.se.controller;

import com.zm.se.entity.Borrow;
import com.zm.se.mapper.EquipmentMapper.EquMapper;
import com.zm.se.mapper.managerMapper.ManagerMapper;
import com.zm.se.service.EquService.impl.EquServiceImpl;
import com.zm.se.service.managerService.impl.ManagerServiceImpl;
import com.zm.se.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("br")
public class BorrowController extends BaseController{
    @Autowired
    ManagerServiceImpl service;
    @Autowired
    EquServiceImpl equService;
    @Autowired
    EquMapper mapper;
    @Autowired
    ManagerMapper managerMapper;

    @RequestMapping("checkborrow")
    public JsonResult<Void> checkBorrow(String username,String num,HttpSession session){
        session.setAttribute("empname",username);
        session.setAttribute("empnum",num);
        service.check(username,num);
        return new JsonResult<>(OK);
    }

    @RequestMapping("checkreturn")
    public JsonResult<Void> checkReturn(String username,String num,HttpSession session){
        session.setAttribute("empname",username);
        session.setAttribute("empnum",num);
        service.check(username,num);
        return new JsonResult<>(OK);
    }
    @RequestMapping("borrowready")
    public JsonResult<String> borrowusername(HttpSession session){
        String username=session.getAttribute("empname").toString();  //sql条件之一
        return new JsonResult<>(OK,username);

    }
    @RequestMapping("returnready")
    public JsonResult<List<Borrow>> returnusername(HttpSession session,Integer page){
        String username=session.getAttribute("empname").toString();  //sql条件之一
        return new JsonResult<>(OK,mapper.showreturn(username,page));

    }
    @RequestMapping("borrow")
    public JsonResult<Void> borrow(Integer enumber,String ename,HttpSession session){
        String empname=session.getAttribute("empname").toString();  //sql条件之一
        String empnum=session.getAttribute("empnum").toString();
        System.err.println(ename+enumber);
        //调用service使用mapper插入一条新记录（empname,empnum，ename，enumber，createdtime,modifiedtime,createduser，modifieduser）
        equService.empborrow(empname,empnum,ename,enumber,getNumFromSession(session));
        return new JsonResult<>(OK);
    }


    @RequestMapping("return")
    public JsonResult<Void> eReturn(Integer id,Integer enumber,HttpSession session){
        System.err.println(id+enumber);
        String managerNum=getNumFromSession(session);
        String manager=managerMapper.findByNum(managerNum).getUsername();
        equService.empreturn(id,enumber,manager);
        return new JsonResult<>(OK);
    }

    @RequestMapping("getnotes")
    public JsonResult<Borrow> change(Integer id){
        System.err.println(id);
        Borrow borrow=mapper.findById(id);
        System.err.println(borrow);
        return new JsonResult<>(OK,borrow);
    }

    @RequestMapping("showBorrowList")
    public JsonResult<List<Borrow>> BorrowList(Integer page){
        List<Borrow> list=mapper.showBorrow(page);
        return new JsonResult<>(OK,list);
    }
    @RequestMapping("selecter")
    public JsonResult<List<String>> selecter(){
        return new JsonResult<>(OK,mapper.selecter());
    }
    @RequestMapping("selectersup")
    public JsonResult<List<String>> selectersup(){
        return new JsonResult<>(OK,mapper.selectersup());
    }
}
